﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReallyReallyReallySimpleRoguelike.Managers
{
    class InputManager
    {

        ConsoleKeyInfo keyInfo;
        ConsoleKeyInfo previousKeyinfo;

        public void ReadKeys()
        {
            previousKeyinfo = keyInfo;

            //Always reads the last key pressed.  If you hit the wrong key, any key not used will cancel if you are quick enough.
            //During the battle there is a one second pause.  If a key is pressed it will be delayed.
            //Add Clearinputstream problem solved.

            //wait for a key to be pressed
            if (Console.KeyAvailable == false)
                keyInfo = new ConsoleKeyInfo();
            else
            {                
                //while keys are available loop
                while (Console.KeyAvailable == true)
                {
                   //true intercepts the key from appearing in the console.
                   keyInfo = Console.ReadKey(true);
                }
            }
        }

        public ConsoleKeyInfo currentKey
        {
            get { return keyInfo; }
        }

        //public bool KeyDown(ConsoleKey key)
        //{

        //    if ( keyInfo.Key == key) 
        //    {    
        //        return true;
        //    }
        //    return false;
        //}

        //Doesn't work the way it should
        public bool KeyPush(ConsoleKey key)
        {
            //If you press the same key twice, doesn't count as new push.
            if (keyInfo.Key == key && previousKeyinfo.Key == 0)
            {
                return true;
            }
            return false;
        }

        //public bool KeyReleased(ConsoleKey key)
        //{
        //    //If you just pressed one key, and now another key is pressed.
        //    if (keyInfo.Key == 0 && previousKeyinfo.Key == key)
        //    {
        //        return true;
        //    }
        //    return false;
        //}

        public static void clearInputStream()
        {
            while (Console.KeyAvailable == true)
            {
                Console.ReadKey();
            }
        }
    }
}
